﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CoreUtils.Common
{
    /// <summary>
    /// Handle exception arising in application
    /// </summary>
    public class ErrorHandler
    {
        public enum ErrorType { ERROR, WARNING, NOTE };

        public static void handlerError(ErrorType type, Exception ex)
        {
            string message = ex.Message + "\n";
            message += "Backtrace: " + "\n";
            message += ex.StackTrace;
            handlerError(type, message);
        }

        public static void handlerError(ErrorType type, String message)
        {
            switch (type)
            {
                case ErrorType.ERROR:
                    FVIL.Log.ProcessLog.Logs.Add(FVIL.Log.LogLevel.Trace, message);
                    if (isDebugging())
                        System.Windows.Forms.MessageBox.Show(message, "Exception", System.Windows.Forms.MessageBoxButtons.OK,
                            System.Windows.Forms.MessageBoxIcon.Error);
                    break;
                case ErrorType.NOTE:
                    System.Windows.Forms.MessageBox.Show(message, Messages.MESSAGE_LABEL, System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Information);
                    break;
                case ErrorType.WARNING:
                    System.Windows.Forms.MessageBox.Show(message, Messages.WARNING, System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Warning);
                    FVIL.Log.ProcessLog.Logs.Add(FVIL.Log.LogLevel.Trace, message);
                    break;
                default:
                    FVIL.Log.ProcessLog.Logs.Add(FVIL.Log.LogLevel.Trace, message);
                    break;
            }
        }

        /// <summary>
        /// Check running mode of application (debug or release)
        /// </summary>
        /// <returns></returns>
        public static bool isDebugging()
        {
            bool debugging = false;
            WellAreWe(ref debugging);
            return debugging;
        }

        [Conditional("DEBUG")]
        private static void WellAreWe(ref bool debugging)
        {
            debugging = true;
        }
    }
}
